"""
统一响应格式
"""
from typing import Any, Optional, Dict
from datetime import datetime
from pydantic import BaseModel


class ErrorDetail(BaseModel):
    """错误详情"""

    code: str
    message: str
    details: Optional[Any] = None


class ApiResponse(BaseModel):
    """统一API响应格式"""

    status: str
    data: Optional[Any] = None
    error: Optional[ErrorDetail] = None
    timestamp: str
    message: Optional[str] = None


def create_response(
    data: Any = None,
    message: Optional[str] = None,
    status: str = "success",
) -> Dict[str, Any]:
    """
    创建成功响应

    Args:
        data: 响应数据
        message: 可选消息
        status: 状态,默认success

    Returns:
        响应字典
    """
    response = {
        "status": status,
        "data": data,
        "timestamp": datetime.now().isoformat(),
    }

    if message:
        response["message"] = message

    return response


def create_error_response(
    code: str,
    message: str,
    details: Optional[Any] = None,
    status_code: int = 400,
) -> Dict[str, Any]:
    """
    创建错误响应

    Args:
        code: 错误代码
        message: 错误消息
        details: 错误详情
        status_code: HTTP状态码

    Returns:
        错误响应字典
    """
    return {
        "status": "error",
        "error": {"code": code, "message": message, "details": details},
        "timestamp": datetime.now().isoformat(),
    }


# 常见错误代码
class ErrorCode:
    """错误代码常量"""

    INVALID_PARAMETER = "INVALID_PARAMETER"
    MODEL_NOT_LOADED = "MODEL_NOT_LOADED"
    PREDICTION_FAILED = "PREDICTION_FAILED"
    DATA_NOT_FOUND = "DATA_NOT_FOUND"
    INTERNAL_ERROR = "INTERNAL_ERROR"
